package com.d404.server;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.alibaba.fastjson.JSON;

import im.beans.Group;
import im.beans.Group_User;
import im.beans.User;
import im.notify.server.Notify;
import manager.MessageManager;
import manager.PersistentManager;

/**
 * 添加好友到群组中
 * Servlet implementation class AddToGroup
 */
//@WebServlet("/AddToGroup")
public class AddToGroup extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddToGroup() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		response.getWriter().append("Served at: ").append(request.getContextPath());
		request.setCharacterEncoding("UTF-8");
		String data = request.getParameter("data");
		Upload upload = JSON.parseObject(data,Upload.class);
		Download download = new Download();
		
		SessionFactory sf = PersistentManager.sharemanager().getSessionFactory();
//		Session session = sf.openSession();
		Session session = sf.getCurrentSession();
		Transaction tx = session.beginTransaction();
		try
		{
			// 在Group_User表中添加新项目
			Group_User gu = new Group_User();
			Group group = (Group) session.get(Group.class, upload.getGroupID());
//			group.setId(upload.getGroupID());
			gu.setGroup(group);
			gu.setCreateGroupUserTimestamp(System.currentTimeMillis());
			User user = new User();
			user.setId(upload.getFriendID());
			gu.setUser(user);
			User user2 = new User();
			user2.setId(group.getOwnerID());
			gu.setOwner(user2);
			
			download.setStatus(Download.SUCCESS);
			session.save(gu);
			session.getTransaction().commit();
			
			// 通知好友被添加到群组
			MessageManager.sendSimpleMessage(upload.getFriendName(), Notify.BEADDTOGROUP);
		}catch(Exception e)
		{
			tx.rollback();
			download.setStatus(Download.FAILED);
			e.printStackTrace();
		}finally {
//			session.close();
		}
		
		System.out.println(JSON.toJSONString(download));
		response.setCharacterEncoding("UTF-8");
		response.getWriter().append(JSON.toJSONString(download));
		
	}


	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	
	 // 上行数据
    public static class Upload
    {
        long friendID;
        long GroupID;
        String friendName;
        String groupName;

        public Upload() {
        }
        
        public Upload(long friendID, long groupID, String friendName, String groupName) {
            this.friendID = friendID;
            GroupID = groupID;
            this.friendName = friendName;
            this.groupName = groupName;
        }

        public long getFriendID() {
            return friendID;
        }

        public void setFriendID(long friendID) {
            this.friendID = friendID;
        }

        public long getGroupID() {
            return GroupID;
        }

        public void setGroupID(long groupID) {
            GroupID = groupID;
        }

        public String getFriendName() {
            return friendName;
        }

        public void setFriendName(String friendName) {
            this.friendName = friendName;
        }

        public String getGroupName() {
            return groupName;
        }

        public void setGroupName(String groupName) {
            this.groupName = groupName;
        }
    }

    // 下行数据
    public static class Download
    {
        public static final int SUCCESS = 1;
        public static final int FAILED = 2;
        int status;

        public int getStatus() {
            return status;
        }

        public void setStatus(int status) {
            this.status = status;
        }
    }

}
